package org.xiyinjihe.platform.util;

import org.apache.commons.codec.binary.Hex;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by wangjun on 2016/7/5.
 */
public class Md5Util {
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * MD5加密方法
     *
     * @param content 加密内容
     * @param type    大小写
     * @return
     */
    public static String encrypt(String content, String type) {
        char encryptChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] contentBytes = content.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(contentBytes);
            byte[] mdBytes = md.digest();
            int mdBytesLength = mdBytes.length;
            char mdlengthChar[] = new char[mdBytesLength * 2];
            int k = 0;
            for (int i = 0; i < mdBytesLength; i++) {
                byte byte0 = mdBytes[i];
                mdlengthChar[k++] = encryptChar[byte0 >>> 4 & 0xf];
                mdlengthChar[k++] = encryptChar[byte0 & 0xf];
            }

            if ("UCase".equals(type)) {
                return new String(mdlengthChar).toUpperCase();
            } else if ("LCase".equals(type)) {
                return new String(mdlengthChar);
            } else {
                return new String(mdlengthChar);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encode(String origin, String charsetname) {
        String resultString = null;
        resultString = new String(origin);
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        if (charsetname == null || "".equals(charsetname)) {
            resultString = Hex.encodeHexString(md.digest(resultString.getBytes()));
        } else {
            try {
                resultString = Hex.encodeHexString(md.digest(resultString.getBytes(charsetname)));
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return resultString;
    }

    public static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    /**
     * J 转换byte到16进制
     *
     * @param b
     * @return
     */
    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static void main(String[] args){
        String s = encrypt("123456","LCase");
        String old = "e10adc3949ba59abbe56e057f20f883e";
        System.out.println(s.equals(old));
    }
}